import 'dart:convert';
import 'dart:io';


import '../dlna_action_result.dart';
import '../dlna_device.dart';

abstract class AbsDLNAAction<T> {
  static final ContentType contentType =
      ContentType('text', 'xml', charset: 'utf-8');

  // Dio httpClient = init();

  HttpClient httpClient = HttpClient();

  DLNADevice dlnaDevice;

  AbsDLNAAction({required this.dlnaDevice}){
    this.dlnaDevice = dlnaDevice;
    httpClient
      ..connectionTimeout = const Duration(seconds: 5)
      ..idleTimeout = const Duration(seconds: 5);
  }

  // static Dio init() {
  //   Dio http = Dio(BaseOptions(
  //       connectTimeout: 5000, receiveTimeout: 5000, sendTimeout: 5000));
  //   final isProd = bool.fromEnvironment('dart.vm.product');
  //   if (!isProd) {
  //     http.interceptors.add(LogInterceptor(
  //         responseBody: false,
  //         requestBody: false,
  //         requestHeader: false,
  //         responseHeader: false));
  //   }
  //   return http;
  // }

  String getControlURL();

  String getXmlData();

  String getSoapAction();

  Future<DLNAActionResult<T>> execute();

  Future<DLNAActionResult<T>> start() async {
    var url = 'http://${Uri.parse(dlnaDevice.location).authority}';
    var controlURL = getControlURL();
    if (!url.endsWith('/')) {
      url = url + ('/');
    }
    if (controlURL.startsWith('/')) {
      controlURL = controlURL.substring(1);
    }
    url = url + controlURL;
    var content = getXmlData();
    var result = DLNAActionResult<T>();
    try {
      // Map<String, dynamic> reqHeaders = Map();
      // reqHeaders["Content-Type"] = contentType.toString();
      // reqHeaders["Content-Length"] = utf8.encode(content).length;
      // reqHeaders["Connection"] = 'Keep-Alive';
      // reqHeaders["Charset"] = 'UTF-8';
      // reqHeaders["Soapaction"] = getSoapAction();
      // Options options = Options(headers: reqHeaders);
      // Response response = await httpClient.post(url,data:content,options: options);
      // result.httpContent = response.data.toString();
      // result.success =
      //     (response.statusCode == HttpStatus.ok && result.httpContent != null);
      var request = await httpClient.postUrl(Uri.parse(url))
        ..headers.contentType = contentType
        ..headers.contentLength = utf8.encode(content).length
        ..headers.add('Connection', 'Keep-Alive')
        ..headers.add('Charset', 'UTF-8')
        ..headers.add('Soapaction', getSoapAction())
        ..write(content);
      var response = await request.close();
      result.httpContent = await response.transform(utf8.decoder).join();
      result.success =
      (response.statusCode == HttpStatus.ok && result.httpContent != null);
    } catch (e) {
      result.success = false;
      result.errorMessage = e.toString();
    }
    return result;
  }
}
